<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
	
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Admin Profile</title>
</head>
<link rel="stylesheet" type="text/css" href="master.css" media="screen" />
    
<?php include("../db.php");

session_start();
check_inactive();
authenticate_admin();

$db = pg_connect('host=dbsrv1 dbname=csc309g7 user=csc309g7 password=aiboid4p');

$id = $_SESSION['userid'];
$otheruser = false;

if($db) 
{
  $query = "select name, email from people where userid = $id";
  
  $result = pg_query($query);

  if ($result) 
  {
  
    while($data = pg_fetch_assoc($result))
	{
    	$sports_interested = $data['sport_interested'];
		if(!$otheruser)
		{
			$username= $data['name'];
			$email = $data['email'];
		}
	
	}
	
}

}

if($_FILES["avatar"])
{
if (($_FILES["avatar"]["type"] == "image/jpeg")&& ($_FILES["avatar"]["size"] < 20000))
{
  if ($_FILES["avatar"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["avatar"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["avatar"]["name"] . "<br />";
    echo "Type: " . $_FILES["avatar"]["type"] . "<br />";
    echo "Size: " . ($_FILES["avatar"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["avatar"]["tmp_name"] . "<br />";

    
      move_uploaded_file($_FILES["avatar"]["tmp_name"],
      "users/avatar$id.jpeg");
      echo "Stored as users/avatar$id.jpeg";
    }
  }
else
  {
  echo "<script type='text/javascript'>alert('Currently we only accept .jpeg image. '); </script>";
  }
}

?>
    
<body >

        <div class="body"> <!-- Class "body" starts-->
                <div id="profile">		

		</div>	

			<div class ="Operations">
                         <p>
	<p>Hello <?php echo $username ?> </p>
	
	<fieldset>
	<legend>Avatar:</legend>
 <form action="" method="POST" enctype="multipart/form-data">
  Upload an image as your avatar:
  <input type="file" name="avatar" />
  <input type="submit" value="Submit"/>
	</form>
	</fieldset>
	 
	  
	 <fieldset>
		<legend>Personal Information:</legend>
	
    <p>Enter the following information that you wish to update:</p>
    
	<table>

	<form action="" method="post">
	Old Password: <input type="Password" name="password_old" /><br />
	New Password: <input type="Password" name="password_new1"/><br />
	Re-enter Password: <input type="password" name="password_new2" /> <br />
	Email: <input type="text" name="email" VALUE="<?php echo $email; ?>"/> <br />
	
	<input type="submit" value="Update" /> 
	</form>
	
	</fieldset>
	 
<?php
	//Check if the user wants to update passwords
	if ($_POST['password_old'] && $_POST['password_new1'] && $_POST['password_new2']) {
		update_info_admin($id , $_POST['password_old'],$_POST['password_new1'],$_POST['password_new2'],$_POST['email']);
	} 
?>
    </p>	
    </div>
	</div> 		
</body>
</html>
